package com.atguigu.gmall.order.receiver;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.model.enums.ProcessStatus;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author gsy
 * @date 2021年09月14日9:43
 * Description
 */
@Component
public class OrderReceiver {
    @Autowired
    private OrderInfoService orderInfoService;

    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void cancelOrder(Long orderId, Channel channel, Message message) {
        try {
            //获取订单id 取消订单
            orderInfoService.cancelOrder(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //e.printStackTrace();
            if (!message.getMessageProperties().isRedelivered()) {
                //第一次接受消息 给一次重新发生的机会
                //参数3:重新返回队列
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } else {
                //重试消息,重新发送消息机会用完
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    @RabbitListener(bindings =
        @QueueBinding(value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY, durable = "true", autoDelete = "false"),
                exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_PAYMENT_PAY),
                key = MqConst.ROUTING_PAYMENT_PAY
        )
    )
    public void receiverUpdateOrderMessage(Long orderId, Channel channel, Message message) {
        try {
            //更新订单表
            //由未支付改为已支付
            orderInfoService.updateOrderStatusPaid(orderId,ProcessStatus.PAID);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //e.printStackTrace();
            if (!message.getMessageProperties().isRedelivered()) {
                //第一次接受消息 给一次重新发生的机会
                //参数3:重新返回队列
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } else {
                //重试消息,重新发送消息机会用完
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

}
